package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.QueryPageBeanCondition;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
	@Reference
	private CheckGroupService checkGroupService;
	//新增检查组
	@RequestMapping("/add")
	public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
		try{
			checkGroupService.add(checkGroup,checkitemIds);
		}catch (Exception e){
			e.printStackTrace();
			//服务调用失败
			return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
		}
		return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
	}
	@PostMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBeanCondition queryPageBeanCondition){
		PageResult pageResult = checkGroupService.pageQuery(queryPageBeanCondition);
		return pageResult;
	}
//	@RequestMapping("/delete")
//	public Result delete(Integer id){
//		return checkGroupService.deleteById(id);
//	}
	//回显
	@RequestMapping("/findGroupById")
	public Result findGroupById(Integer id){
		try{
			CheckGroup checkGroup = checkGroupService.findGroupById(id);
			return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
		}catch (Exception e){
			e.printStackTrace();
			//服务调用失败
			return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}
	@RequestMapping("/findItemById")
	public Result findItemById(Integer id){
		try{
			List<Integer> checkitemIds = checkGroupService.findItemById(id);
			return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
		}catch (Exception e){
			e.printStackTrace();
			//服务调用失败
			return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
		}
	}
	@RequestMapping("/edit")
	public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
		try{
			checkGroupService.edit(checkGroup,checkitemIds);
		}catch (Exception e){
			e.printStackTrace();
			//服务调用失败
			return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
		}
		return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
	}
	@RequestMapping("/findAll")
	public Result findAll(){
		try{
			List<CheckGroup> checkGroupList = checkGroupService.findAll();
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
		}catch (Exception e){
			e.printStackTrace();
			//服务调用失败
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}
}
